import {
  CHANGE_DETAIL_COMMENT,
  CHANGE_NEW_COMMENT,
  CHANGE_DETAIL_NOTE,
  CHANGE_DETAIL_NOTE_ID,
  CHANGE_DETAIL_NOTE_USER_ID,
} from "./constant";
import { getNoteDetail, getNoteComment } from "@/services";

const changeDetailCommentAction = (data) => ({
  type: CHANGE_DETAIL_COMMENT,
  data,
});

export const getDetailCommentAction = (noteId) => {
  return (dispatch) => {
    getNoteComment(noteId).then((res) => {
      dispatch(changeDetailCommentAction(res.data.list));
    });
  };
};

const changeNewCommentAction = (data) => ({
  type: CHANGE_NEW_COMMENT,
  data,
});

export const setNewCommentAction = (data) => {
  return (dispatch) => {
    dispatch(changeNewCommentAction(data));
  };
};

const changeDetailNoteAction = (data) => ({
  type: CHANGE_DETAIL_NOTE,
  data,
});

export const getDetailNoteAction = (noteId) => {
  return (dispatch) => {
    getNoteDetail(noteId).then((res) => {
      dispatch(changeDetailNoteAction(res));
    });
  };
};

// 设置笔记ID
const changeNoteIdAction = (data) => ({
  type: CHANGE_DETAIL_NOTE_ID,
  data,
});
export const setNoteIdAction = (noteId) => {
  return (dispatch) => {
    dispatch(changeNoteIdAction(noteId));
  };
};

// 设置笔记的发布者的ID
const changeNoteUserIdAction = (data) => ({
  type: CHANGE_DETAIL_NOTE_USER_ID,
  data,
});
export const setNoteUserIdAction = (userId) => {
  return (dispatch) => {
    dispatch(changeNoteUserIdAction(userId));
  };
};
